package com.industrial.premu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.premu.domain.BigPingCompanyEvaluateInfo;
import com.industrial.premu.domain.CityLevelCompanyCount;
import com.industrial.premu.domain.KvDomain;
import com.industrial.premu.domain.PreMuBenefitEvaluation;
import com.industrial.premu.mapper.BigPingMapper;
import com.industrial.premu.service.IBigPingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 亩产大屏
 * @author sangguangcan
 */
@Service
public class BigPingServiceImpl implements IBigPingService {
    @Autowired
    private BigPingMapper bigPingMapper;


    /**
     * 查询企业评价信息
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryCompanyEvaluateInfo(PreMuBenefitEvaluation params) {

        //查询企业层级的数量
        List<BigPingCompanyEvaluateInfo> companyMjLevelInfo = bigPingMapper.queryCompanyEvaluateMjLevelInfo(params);

        //查询企业总数
        BigPingCompanyEvaluateInfo companyTotal = bigPingMapper.queryCompanyEvaluateTotalInfo(params);
        Map<String, Object> resultMap = new HashMap<>(16);
        if(companyMjLevelInfo != null && !companyMjLevelInfo.isEmpty()){
            resultMap = companyMjLevelInfo.stream().collect(Collectors.toMap(BigPingCompanyEvaluateInfo::getCompanyLevel, BigPingCompanyEvaluateInfo::getCompanyCount));
        }
        if(companyTotal != null){
            Map<String, Object> companyTotalMap = BeanUtil.beanToMap(companyTotal);
            resultMap.putAll(companyTotalMap);
        }
        return resultMap;
    }

    /**
     * 查大屏中工业总产值 销售收入等信息
     *
     * @param years
     * @return
     */
    @Override
    public Map<String, Object> queryPreMuReactionInformation(PreMuBenefitEvaluation params) {
        Map<String,Object> result = bigPingMapper.queryPreMuReactionInformation(params);
        return result;
    }

    /**
     * 查询地市等级分布
     *
     * @param years
     * @return
     */
    @Override
    public List<Map<String,String>> queryCityLevel(Map<String,String> params) {

        if(StringUtils.isBlank(params.get("years")) || StringUtils.isBlank(params.get("mjLevel"))){
            throw new RuntimeException("请传入年份或等级");
        }

        //查询城市的等级的企业数量
        List<CityLevelCompanyCount> cityLevelCompanyCounts = bigPingMapper.queryCityLevelCompanyCount(params);

        List<Map<String,String>> resultList = new ArrayList<>();


        for(CityLevelCompanyCount p : cityLevelCompanyCounts){
            Map<String,String> temp = new HashMap<>(16);
            temp.put("name",p.getCityName());
            temp.put("value",p.getCompanyCount());
            resultList.add(temp);
        }

        return resultList;
    }

    /**
     * 查询地图上的数据
     *
     * @param years
     * @return
     */
    @Override
    public List<Map> queryMapData(PreMuBenefitEvaluation params) {
        List<Map> map = bigPingMapper.queryMapData(params);
        return map;
    }

    /**
     * 查询各行业分布统计TOP5
     *
     * @param params indexName 指标名称
     * @return
     */
    @Override
    public Map<String,Object> queryIndustryTop5(Map<String, String> params) {

        List<KvDomain> kvDomains = bigPingMapper.queryIndustryTop5(params);

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        for(KvDomain k : kvDomains){
            String name = k.getName();
            String resultValue = k.getResultValue();

            xAxis.add(name);
            yAxis.add(resultValue);
        }

        Map<String,Object> result = new HashMap<>(16);
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }

    /**
     * 查询地市企业指标分析
     *
     * @param params indexName 指标名称
     * @return
     */
    @Override
    public Map<String,Object> queryCityIndexTop5(Map<String, String> params) {
        List<KvDomain> kvDomains = bigPingMapper.queryCityIndexTop5(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        for(KvDomain k : kvDomains){
            String name = k.getName();
            String resultValue = k.getResultValue();

            xAxis.add(name);
            yAxis.add(resultValue);
        }

        Map<String,Object> result = new HashMap<>(16);
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }

    /**
     * 查询亩产中的所有年份
     *
     * @return
     */
    @Override
    public List<String> queryAllDataYears() {
        return bigPingMapper.queryAllDataYears();
    }
}
